package com.yohann.algorithm.kmp;

import java.util.ArrayList;
import java.util.Date;

/**
 * <p>
 * 暴力匹配
 * </p>
 *
 * @author Yohann
 * @since 2021/1/18 20:46
 */
public class ViolenceMatch {
    public static void main(String[] args) {
        String str1 = "3434dsfghasdfdsadfg34sdff34dsfghasdfdsadfg34sdff34dsfghasdfdsadfg34sdffdsf3435";
        String str2 = "3435";

        long time1 = System.currentTimeMillis();
        System.out.println(violenceMatch(str1, str2));
        long time2 = System.currentTimeMillis();
        System.out.println(time2 - time1);
    }

    private static int violenceMatch(String str1, String str2) {
        char[] s1 = str1.toCharArray();
        char[] s2 = str2.toCharArray();

        int length1 = s1.length;
        int length2 = s2.length;

        //指向s1
        int i = 0;
        //指向s2
        int j = 0;

        while (i < length1 && j < length2) {
            if (s1[i] == s2[j]) {
                i++;
                j++;
            } else {
                i = i - j + 1;
                j = 0;
            }
        }

        if (j == length2) {
            return i - j;
        } else {
            return -1;
        }
    }
}